<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta charset="utf-8">
  <title>编辑规格图片</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" id="id" value="${detail.id }"/>
    <input type="hidden" name="imgUrl" id="imgUrl" value="${detail.imgUrl }"/>
    <div class="layui-form-item">
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-upload">
								<button type="button" class="layui-btn" id="test-upload-normal">上传图片</button>
								<div class="layui-upload-list">
									<img class="layui-upload-img" id="test-upload-normal-img"
										style="width: 176px; higth: 176px">
									<p id="test-upload-demoText"></p>
								</div>
							</div>
							<blockquote class="layui-elem-quote">
							图片尺寸：88 * 88
							</blockquote>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
    <div class="layui-form-item layui-hide">
      <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
      <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
  </div>

  <script src="../../../layuiadmin/layui/layui.js"></script>  
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form','upload'], function(){
    var $ = layui.$
    ,form = layui.form
    ,upload = layui.upload;
    
    
    //监听提交
    form.on('submit(layuiadmin-app-form-submit)', function(data){
      var field = data.field; //获取提交的字段
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  

      //提交 Ajax 成功后，关闭当前弹层并重载表格
      $.ajax({
    	  url:'/product/updateProductDetailImg',
          type:'post',
    	  dataType:'json',
    	  data:{
    		  'id':field.id,
    		  'imgUrl':field.imgUrl
    	  },
    	  success:function(res){
    		  if(res.code == 0){
    			  layer.msg('保存成功');
    		      parent.layui.table.reload('skuImg'); //重载表格
    		      parent.layer.close(index); //再执行关闭 
    		  }else{
    			  layer.msg('保存失败');
    		  }
    	  }
      });
    });
    
    
    //普通图片上传
    var uploadInst = upload.render({
      elem: '#test-upload-normal'
      ,url: '/base/uploadOneFile'
      ,before: function(obj){
        //预读本地文件示例，不支持ie8
        obj.preview(function(index, file, result){
          $('#test-upload-normal-img').attr('src', result); //图片链接（base64）
        });
      }
      ,done: function(res){
        if(res.status){
        	//上传成功
        	$("#imgUrl").val(res.data);
        }else{
        	//如果上传失败
        	return layer.msg('上传失败');
        }
      }
      ,error: function(){
        //演示失败状态，并实现重传
        var demoText = $('#test-upload-demoText');
        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
        demoText.find('.demo-reload').on('click', function(){
          uploadInst.upload();
        });
      }
    });
    
  })
  </script>
</body>
</html>
